<?php
namespace Org;
use Think\Controller;
class Autoload extends Controller{
    //ThinkPHP的构造函数
    public function _initialize() {
        //$this->url_jump(); //如果是手机端则跳转
        //$this->maintaining(); //维护中
        //$this->auto_login(); //自动登录
        //$this->auto_member_info=$this->get_member_info();
        //$this->member_id=$this->auto_member_info['member_id'];
        //print_r($this->member_info);exit;
        $this->assign('uid',session('uid'));
        $this->assign('email',session('email'));
        //设置cookie
        if(session_id()!=''){
            cookie('session_id',session_id(),array('domain' =>'.think.local'));  //二级域名设置
        }else{
            $session_id=cookie('session_id');
            cookie('PHPSESSID',$session_id);
        }
        //dump(session());exit;
        //dump($member_info);exit;
        //echo $member_info['member_id'];exit;
    }
    //维护中
    private function maintaining(){
        echo '网站维护中，造成不便，敬请谅解,预计2016.5.26 17:15重新开启';exit;
    }
    //自动登录 此处根据UC同步登录时传过来的uid进行处理，如果已经存在用户，则自动登录，否则，跳到完善资料页面，完善资料后写入本地数据库
    //此处改为仅其他站推送过来的
    private function auto_login(){
    	//dump(cookie());exit;
        //cookie('uid',null);exit;
        //判断 cookie uid在而 member_id不在 则写入数据 并提示完善资料
        if(cookie('uid')!=''&&cookie('member_id')==''){
            $uid=cookie('uid');
            //查询是否存在
            $Member=M('Member');
            $count=$Member->where('member_id='.$uid)->count();
            if($count==0){
                //连接UC端
                $UC=M('Members','zjy_','DB_CONFIG');
                $usertype=$UC->where('uid='.$uid)->getField('usertype');
                $username=$UC->where('uid='.$uid)->getField('username');
                $password=$UC->where('uid='.$uid)->getField('password');
                $email=$UC->where('uid='.$uid)->getField('email');
                $mobile=$UC->where('uid='.$uid)->getField('mobile');
                //写入数据库
                $data['member_id']=$uid;
                $data['uc_username']=$username;
                $data['uc_usertype']=$usertype;
                $data['member_passwd']=$password;
                $data['member_email']=$email;
                $data['member_mobile']=$mobile;
                $Member->add($data);
                //设置session cookie
                session('member_id',$uid,3600);
                cookie('member_id',$uid,3600);
                session('member_email',$email,3600);
                cookie('member_email',$email,3600);
                session('member_mobile',$mobile,3600);
                cookie('member_mobile',$mobile,3600);
            }
        }
    }
    //获取 用户信息
    private function get_member_info(){
        //
        //设置 member_info
        if(cookie('member_id')){
            $member_info['member_id']=cookie('member_id');
        }
        if(cookie('member_email')){
            $member_info['member_email']=cookie('member_email');
        }
        if(cookie('member_mobile')){
            $member_info['member_mobile']=cookie('member_mobile');
        }
        if(session('member_id')){
            $member_info['member_id']=session('member_id');
        }
        if(session('member_email')){
            $member_info['member_email']=session('member_email');
        }
        if(session('member_mobile')){
            $member_info['member_mobile']=session('member_mobile');
        }
        //dump(cookie());exit;
        //dump($member_info);exit;
        return $member_info;
    }

    //如果是手机端则跳转
    private function url_jump(){
        if($this->checkmobile()){
            header('location:http://m.b2b.local');exit;
        }
    }

    //检测是否手机
    private function checkmobile(){
        global $_G;
        $mobile = array();
        //各个触控浏览器中$_SERVER['HTTP_USER_AGENT']所包含的字符串数组
        static $touchbrowser_list = array('iphone', 'android', 'phone', 'mobile', 'wap', 'netfront', 'java', 'opera mobi', 'opera mini',
            'ucweb', 'windows ce', 'symbian', 'series', 'webos', 'sony', 'blackberry', 'dopod', 'nokia', 'samsung',
            'palmsource', 'xda', 'pieplus', 'meizu', 'midp', 'cldc', 'motorola', 'foma', 'docomo', 'up.browser',
            'up.link', 'blazer', 'helio', 'hosin', 'huawei', 'novarra', 'coolpad', 'webos', 'techfaith', 'palmsource',
            'alcatel', 'amoi', 'ktouch', 'nexian', 'ericsson', 'philips', 'sagem', 'wellcom', 'bunjalloo', 'maui', 'smartphone',
            'iemobile', 'spice', 'bird', 'zte-', 'longcos', 'pantech', 'gionee', 'portalmmm', 'jig browser', 'hiptop',
            'benq', 'haier', '^lct', '320x320', '240x320', '176x220');
        //window手机浏览器数组【猜的】
        static $mobilebrowser_list = array('windows phone');
        //wap浏览器中$_SERVER['HTTP_USER_AGENT']所包含的字符串数组
        static $wmlbrowser_list = array('cect', 'compal', 'ctl', 'lg', 'nec', 'tcl', 'alcatel', 'ericsson', 'bird', 'daxian', 'dbtel', 'eastcom',
            'pantech', 'dopod', 'philips', 'haier', 'konka', 'kejian', 'lenovo', 'benq', 'mot', 'soutec', 'nokia', 'sagem', 'sgh',
            'sed', 'capitel', 'panasonic', 'sonyericsson', 'sharp', 'amoi', 'panda', 'zte');
        $pad_list = array('pad', 'gt-p1000');
        $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
        if ($this->dstrpos($useragent, $pad_list)) {
            return false;
        }
        if (($v = $this->dstrpos($useragent, $mobilebrowser_list, true))) {
            $_G['mobile'] = $v;
            return '1';
        }
        if (($v = $this->dstrpos($useragent, $touchbrowser_list, true))) {
            $_G['mobile'] = $v;
            return '2';
        }
        if (($v = $this->dstrpos($useragent, $wmlbrowser_list))) {
            $_G['mobile'] = $v;
            return '3'; //wml版
        }
        $brower = array('mozilla', 'chrome', 'safari', 'opera', 'm3gate', 'winwap', 'openwave', 'myop');
        if ($this->dstrpos($useragent, $brower)) return false;
        $_G['mobile'] = 'unknown';
        //对于未知类型的浏览器，通过$_GET['mobile']参数来决定是否是手机浏览器
        if (isset($_G['mobiletpl'][$_GET['mobile']])) {
            return true;
        } else {
            return false;
        }
    }

    //判断手机相关方法
    private function dstrpos($string, $arr, $returnvalue = false){
        if (empty($string)) return false;
        foreach ((array)$arr as $v) {
            if (strpos($string, $v) !== false) {
                $return = $returnvalue ? $v : true;
                return $return;
            }
        }
        return false;
    }

}